package org.wzq.utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.net.SocketTimeoutException;


/**
 * @author 黄黎
 * @version 创建时间：2014年5月8日 下午3:50:32
 *          HTTP连接
 */
public class HttpConnection {
//    public static String post(String url, List<BasicNameValuePair> paramss) {
//        try {
//            HttpClient client = new DefaultHttpClient();
//            HttpParams params = client.getParams();
//            HttpConnectionParams.setSoTimeout(params, 3000);
//            HttpConnectionParams.setConnectionTimeout(params, 3000);
//            HttpPost post = new HttpPost(url);
//
//            // post.addHeader("Content-Type", "application/json");
//            post.addHeader("charset", HTTP.UTF_8);
//            post.setEntity(new UrlEncodedFormEntity(paramss, HTTP.UTF_8));
//            HttpResponse response = client.execute(post);
//            int code = response.getStatusLine().getStatusCode();
//            String resString = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
//            return resString;
//        } catch (SocketTimeoutException e) {
//            e.printStackTrace();
//        } catch (ConnectTimeoutException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    public static String post(String url, String entity) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpParams params = client.getParams();
            HttpConnectionParams.setSoTimeout(params, 3000);
            HttpConnectionParams.setConnectionTimeout(params, 3000);
            HttpPost post = new HttpPost(url);

            post.addHeader("charset", HTTP.UTF_8);
            post.setEntity(new StringEntity(entity));
            HttpResponse response = client.execute(post);
            String resString = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            return resString;
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
        } catch (ConnectTimeoutException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

//    public static String put(String url, List<BasicNameValuePair> paramss) {
//        try {
//            HttpClient client = new DefaultHttpClient();
//            HttpParams params = client.getParams();
//            HttpConnectionParams.setSoTimeout(params, 3000);
//            HttpConnectionParams.setConnectionTimeout(params, 3000);
//            HttpPut httpPut = new HttpPut(url);
//
//            // post.addHeader("Content-Type", "application/json");
//            httpPut.addHeader("charset", HTTP.UTF_8);
//            httpPut.setEntity(new UrlEncodedFormEntity(paramss, HTTP.UTF_8));
//            HttpResponse response = client.execute(httpPut);
//            int code = response.getStatusLine().getStatusCode();
//            String resString = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
//            return resString;
//        } catch (SocketTimeoutException e) {
//            e.printStackTrace();
//        } catch (ConnectTimeoutException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    public static String put(String url, String body) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpParams params = client.getParams();
            HttpConnectionParams.setSoTimeout(params, 3000);
            HttpConnectionParams.setConnectionTimeout(params, 3000);
            HttpPut httpPut = new HttpPut(url);

            httpPut.addHeader("charset", HTTP.UTF_8);
            httpPut.setEntity(new StringEntity(body, HTTP.UTF_8));
            HttpResponse response = client.execute(httpPut);
            String resString = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            return resString;
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
        } catch (ConnectTimeoutException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String get(String url) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpParams params = client.getParams();
            HttpConnectionParams.setSoTimeout(params, 3000);
            HttpConnectionParams.setConnectionTimeout(params, 3000);
            HttpGet get = new HttpGet(url);

            HttpResponse response = client.execute(get);
            String resString = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            return resString;
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
        } catch (ConnectTimeoutException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

